require('../css/register.less')
let captcha1 = new CaptchaMini({
	fontSize: 55,
	lineWidth: 5,
	lineNum: 6,
})
let codes
captcha1.draw(document.querySelector('#captcha1'), function (code) {
	codes = code
})
let uname = document.querySelector('main #uname')
let cpat = document.querySelector('main #cpat')
let pass = document.querySelector('main #pass')
let passt = document.querySelector('main #passt')
uname.addEventListener('input', unamech)
function unamech() {
	if (window.utils.cellPhoneNumber(uname.value)) {
		uname.style.color = 'springgreen'
		return true
	} else {
		uname.style.color = 'red'
		return false
	}
}
cpat.addEventListener('input', cpatch)
function cpatch() {
	if (cpat.value.toLowerCase() === codes.toLowerCase()) {
		cpat.style.color = 'springgreen'
		return true
	} else {
		cpat.style.color = 'red'
		return false
	}
}
pass.addEventListener('input', passch)
function passch() {
	if (window.utils.checkPhoneNum(pass.value)) {
		pass.style.color = 'springgreen'
		passt.disabled = false
		return true
	} else {
		pass.style.color = 'red'
		passt.disabled = true
		return false
	}
}
passt.addEventListener('input', passtch)
function passtch() {
	if (passt.value == pass.value) {
		passt.style.color = 'springgreen'
		return true
	} else {
		passt.style.color = 'red'
		return false
	}
}
let btn = document.querySelector('main .btn')
btn.addEventListener('click', function () {
	let flage = [unamech(), cpatch(), passch(), passtch()]
	let arr = flage.every(function (item) {
		return item
	})
	console.log(arr)
	if (arr === true) {
		window.$http.post(
			'/users/add',
			{
				account: uname.value,
				password: pass.value,
			},
			function (res) {
				console.log(res)
				if (res.status == 0) {
					window.utils.showToast(0, res.msg, function () {
						window.$http.post(
							'/users/login',
							{
								account: uname.value,
								password: pass.value,
							},
							function (res) {
								if (res.status === 0) {
									localStorage.setItem(
										'users',
										JSON.stringify(res.data.user)
									)
									window.utils.showToast(
										0,
										'正在登录请稍候',
										function () {
											window.location.href = `./index.html`
										}
									)
								}
							}
						)
					})
				} else {
					window.utils.showToast(1, res.msg)
				}
			}
		)
	} else {
		window.utils.showToast(1, res.msg)
	}
})
